new Vue({
	el: '#app',
	data: {
		listData: [],
		itemData: {},
		rules: {
			id: [
				{ required: true, message: '编号不能为空', trigger: 'blur' },
			],
      	},
		itemDialogVisible: false,
		idNewAddFlag: true,
	},
	methods: {
		// 删除
		delItem (item) {
			console.log(33);
			this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				axiosObj.delItem(item.id, (res) => {
					if(res.flag === '0000') {
						this.$message.success('操作成功');
						this.getList();
					}
				})
			}).catch(() => {
				console.log(22);    
			});
		},
		// 编辑
		editItem (item) {
			axiosObj.getItem(item.id, (res) => {
				if(res.flag === '0000') {
					this.idNewAddFlag = false;
					this.itemDialogVisible = true;
					this.itemData = res.data;//JSON.parse(JSON.stringify(item));
				}
			})
		},
		// 新增
		addItem () {
			this.idNewAddFlag = true;
			this.itemDialogVisible = true;
			this.itemData = {};
		},
		// 弹窗-确定
		clickSure () {
			this.$refs['ruleForm'].validate((valid) => {
				if (valid) {
					if(this.idNewAddFlag) {
						this.forEditItem('addItem');
					}else {
						this.forEditItem('editItem');
					}
				}
			});
		},
		getList () {
			axiosObj.getList((res) => {
				if(res.flag === '0000') {
					this.listData = res.data;
				}
			});
		},
		forEditItem (methods) {
			axiosObj[methods]({
				id: this.itemData.id,
				username: this.itemData.username,
				age: this.itemData.age,
			}, (res) => {
				if(res.flag === '0000') {
					this.$message.success('操作成功');
					this.itemDialogVisible = false;
					this.getList();
				}else {
					this.$message.error(res.msg);
				}
			})
		},
	},
	mounted () {
		setTimeout(() => {
			this.getList();
		}, 500)
	}

})